package com.cdshangpai.photoapp.ui.hotal;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSONObject;
import com.cdshangpai.photoapp.PhotoAppBaseActivity;
import com.cdshangpai.photoapp.R;
import com.cdshangpai.photoapp.adapters.DataBean;
import com.cdshangpai.photoapp.databinding.ActivityHotalDetailBinding;
import com.cdshangpai.photoapp.ui.view.DragImageView;
import com.cdshangpai.utils.db.beans.Hotal;
import com.cdshangpai.utils.storage.ImageManager;
import com.cdshangpai.utils.system.ScreenParams;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class HotalDetailActivity extends PhotoAppBaseActivity {
    ActivityHotalDetailBinding binding;

    List<DataBean> images = new ArrayList<>();
    ImageManager imageManager;
    Hotal hotal;
    public static HashSet<Integer> formCaseIds=new HashSet<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getParams();

        binding= DataBindingUtil.setContentView(this,R.layout.activity_hotal_detail);

        imageManager = new ImageManager();
        binding.setView(this);
        binding.setHotal(hotal);

        DragImageView iv=(DragImageView)binding.hotalBigImage;
        iv.setmActivity(this);
        iv.setScreen_W(ScreenParams.screenWidth);
        iv.setScreen_H(ScreenParams.screenHeight);

        setImageList();
        updateCheckState();

    }

    public void goBack(View view)
    {
        this.finish();
    }

    private void getParams()
    {
        Intent intent=getIntent();
        String param=intent.getStringExtra("param");
        hotal= JSONObject.parseObject(param, Hotal.class);

        if(!TextUtils.isEmpty(hotal.getPhotos()))
        {
            images=new ArrayList<>();
            String[] photoArr=hotal.photos.split(",");
            for(int i=0;i<photoArr.length;i++)
            {
                images.add(new DataBean(0,photoArr[i]));
            }
        }
    }


    private void setImageList() {

        BannerImageAdapter adapter = new BannerImageAdapter<DataBean>(images) {
            @Override
            public void onBindView(BannerImageHolder holder, DataBean data, int position, int size) {
                Bitmap bitmap = imageManager.loadImageToBitmap(data.imageUrl);
                holder.imageView.setImageBitmap(bitmap);
                holder.imageView.setLongClickable(true);
                holder.imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                holder.imageView.setTag(position);

                holder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position=new Integer(v.getTag().toString());
                        DataBean bean=(DataBean)binding.hotalDetailInnerBanner.getAdapter().getData(position);
                        showBigImage(bean.imageUrl);
                        return false;
                    }
                });
            }
        };
        binding.hotalDetailInnerBanner.setAdapter(adapter)
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(this));
    }

    private Boolean isFormCheck()
    {
        return formCaseIds.contains(hotal.id);
    }

    public ArrayList<Integer> getSelectedCaseIds()
    {
        ArrayList<Integer> ids=new ArrayList<>();
        for(Integer id:formCaseIds)
        {
            ids.add(id);
        }
        return ids;
    }

    public void onFormSelect(View view)
    {
        if(isFormCheck())
        {
            formCaseIds.remove(hotal.id);
        }
        else
        {
            formCaseIds.add(hotal.id);
        }
        updateCheckState();
    }

    public static List<Integer> getSelectedIds()
    {
        List<Integer> ids=new ArrayList<>();
        for(Integer id:formCaseIds)
        {
            ids.add(id);
        }
        return ids;
    }

    public static void cleanSelected()
    {
        formCaseIds.clear();
    }

    public void updateCheckState()
    {
        binding.setFormChecked(isFormCheck());
    }

    //显示单大图功能区
    public void showBigImage(String file)
    {
        Bitmap temp=imageManager.loadImageToBitmap(file);
        Bitmap bitmap=imageManager.scale(temp,5760);
        binding.hotalBigImage.setImageBitmap(bitmap);
        binding.setShowBigPic(true);
    }
    public void hideBigImage()
    {
        binding.setShowBigPic(false);
    }

}